<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  name: 'LineChart',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart1'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    item: {
      type: Object
    },
    x_data: {
      type: Array
    },
    y_data: {
      type: Array
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    'x_data': function() {
      this.initChart()
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        title: {
          text: '课堂心率曲线',
          textStyle: {
            color: 'black',
            fontWeight: 'bold'
          }

        },
        backgroundColor: 'white',
        tooltip: {
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          },
          trigger: 'axis',
          formatter: function(param) {
            // console.log(param)
            param = param[0]
            // console.log(param)
            return [
              '心率' + '<br/>' + '<hr size=1 style="margin: 3px 0">' +
              '<h1 style="font-size:14px;color:white;margin:0 25px 0 0">' + param.data + '</h1>'
            ].join('')
          }
        },
        visualMap: {
          top: 50,
          right: 10,
          pieces: [{
            // gt: 0,
            lte: 50,
            color: '#CCCCCC'
          }, {
            gt: 50,
            lte: 100,
            color: '#39ACFF'
          }, {
            gt: 100,
            lte: 150,
            color: '#6DD401'
          }],
          outOfRange: {
            color: '#999'
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: this.x_data,
          axisLine: {
            lineStyle: {
              color: '#CCCCCC'
            }
          },
          axisLabel: {
            textStyle: {
              color: 'black'// 坐标值得具体的颜色
            }
          }
        },
        yAxis: {
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed'
            }
          },
          axisLine: {
            lineStyle: {
              color: 'white'
            }
          },
          axisLabel: {
            textStyle: {
              color: 'black'// 坐标值得具体的颜色

            }
          }
        },
        series: [
          {
            data: this.y_data,
            type: 'line',
            areaStyle: {}

          },
          {
            // data: ['-', '-', '-', '-', '-', '-', 80, 90, 140, 110, 80, 90, 60],
            type: 'line'
          }
        ]
      })
    }
  }
}
</script>
